package com.example.b_308.myapplication.view.user.register;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.controller.UserController;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.model.base.OfficeContent;
import com.example.b_308.myapplication.view.base.BaseFragment;
import com.example.b_308.myapplication.view.user.login.IUserView;

/**
 * Created by B-308 on 2017/11/23.
 */
public class VerifyFragment extends BaseFragment implements View.OnClickListener, IUserView {

    EditText et_reg_phone, et_reg_vertify;
    ImageView iv_reg_phone_delete, iv_reg_vertify_delete;
    Button btn_click_gain, but_next;
    UserController userController;


    @Override
    protected void init() {

        btn_click_gain = findViewById(R.id.btn_click_gain);
        et_reg_phone = findViewById(R.id.et_reg_phone);
        et_reg_vertify = findViewById(R.id.et_reg_vertify);
        userController = new UserController(this);
        iv_reg_phone_delete = findViewById(R.id.iv_reg_phone_delete);
        iv_reg_vertify_delete = findViewById(R.id.iv_reg_vertify_delete);
        but_next = findViewById(R.id.but_next);
        iv_reg_phone_delete.setOnClickListener(this);
        iv_reg_vertify_delete.setOnClickListener(this);
        but_next.setOnClickListener(this);
        btn_click_gain.setOnClickListener(this);
        et_reg_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (et_reg_phone.length() == 0) {
                    but_next.setSelected(false);
                    iv_reg_phone_delete.setVisibility(View.GONE);
                } else {
                    but_next.setSelected(true);
                    iv_reg_phone_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_reg_vertify.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (et_reg_vertify.length() == 0) {
                    iv_reg_vertify_delete.setVisibility(View.GONE);
                } else {
                    iv_reg_vertify_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }


    @Override
    protected View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_verity, null, false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_next:
                if (et_reg_phone.length() != 0) {
                    if (et_reg_vertify.length() != 0) {
                        ((RegisterActivity) getActivity()).next(this, et_reg_phone.getText().toString());
                    }else{
                        fail("验证码不能为空!");
                    }
                } else {
                    fail("手机号码不能为空！");
                }


                break;
            case R.id.btn_click_gain:
                userController.getCode(et_reg_phone.getText().toString(), OfficeContent.REGISTER);
                break;
            case R.id.iv_reg_phone_delete:
                et_reg_phone.setText("");
                break;
            case R.id.iv_reg_vertify_delete:
                et_reg_vertify.setText("");
                break;
        }
    }

    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {

    }

    @Override
    public void getCodeSuccess() {
        new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        int i = 60;

                        while (i > 0) {
                            i -= 1;
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            final int finalI = i;
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (finalI > 0) {
                                        btn_click_gain.setText("" + (finalI));
                                        btn_click_gain.setSelected(true);
                                        btn_click_gain.setClickable(false);
                                    } else {
                                        btn_click_gain.setText("重新获取");
                                        btn_click_gain.setSelected(false);
                                        btn_click_gain.setClickable(true);

                                    }
                                }
                            });
                        }
                    }
                };
                thread.run();
                return null;
            }
        }.execute();

    }
}
